﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Anthony.Library.Entensions
{
    public static class DataTypeExtensions
    {

        /// <summary>
        /// Extension method to remove \n and \r
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public static string RemoveLineBreaks(this string s)
        {
            return s.Replace("\n", string.Empty).Replace("\r", string.Empty);
        }


        /// <summary>
        /// Converts a byte array of UTF-8 encoded values to a string.
        /// </summary>
        /// <param name="characters"></param>
        /// <returns></returns>
        public static string UTF8ByteArrayToString(this byte[] chars)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            return encoding.GetString(chars);
        }


        /// <summary>
        /// Converts a String to UTF8 byte array.
        /// </summary>
        /// <param name="pXmlString"></param>
        /// <returns></returns>
        public static byte[] ToUTF8ByteArray(this string s)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            return encoding.GetBytes(s);
        }


        #region ToDate

        /// <summary>
        /// Parses string as date with culture info en-US.
        /// Returns default date if string is null or empty.
        /// </summary>
        /// <param name="date"></param>
        /// <param name="defaultDate"></param>
        /// <returns></returns>
        public static DateTime ToDate(this string date, DateTime defaultDate)
        {
            if (string.IsNullOrEmpty(date))
                return defaultDate;

            var culture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
            var d = DateTime.Parse(date, culture);
            return d;
        }

        #endregion
    }
}
